package com.jic.member.impl;

import com.jic.common.base.vo.RestResult;
import com.jic.member.entity.MememberIntegralLog;
import com.jic.member.entity.MememberUser;
import com.jic.member.mapper.MememberIntegralLogMapper;
import com.jic.member.mapper.MememberUserMapper;
import com.jic.member.provider.service.MememberIntegralExchangeLog;
import com.jic.member.request.MememberIntegralActionRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;



@Slf4j
@Component
public class MememberIntegralExchangeLogImpl implements MememberIntegralExchangeLog {


    @Autowired
    private MememberUserMapper mememberUserMapper;


    @Autowired
    private MememberIntegralLogMapper mememberIntegralLogMapper;

    /**
     * 积分消耗/增加
     *
     * @param request
     * @return
     */
    @Override
    @Transactional
    public RestResult<?> updMemberIntegralInfo(MememberIntegralActionRequest request) {
        log.info("开始积分新增/消耗操作==》入参,{}", request.toString());
        RestResult restResult = new RestResult();

        return restResult;

    }

    @Override
    public RestResult<?> getMemberIntegralInfo(String thirdUserId) {
        log.info("开始积分查询操作==》入参,{}", thirdUserId);
        RestResult restResult = new RestResult();

        return restResult;
    }

    private Boolean addLog(String thirdUserId, BigDecimal integral, Integer changeTypes, MememberUser member, Integer type) {
        if (type == 1) {//直接新增log
            MememberIntegralLog log = new MememberIntegralLog();
            log.setThirdUserId(thirdUserId);
            log.setMemberUserId(member.getId());
            log.setIntegral(integral);
            log.setActionType(2);
            log.setActionTypeDes("积分修改");
            log.setCreateTime(new Date());
            log.setUpdateTime(new Date());
            log.setChangeTypes(changeTypes);
            log.setChangeTime(new Date());
            log.setStoreMember(1);
            log.setDeptId(member.getDeptId());
            long count = mememberIntegralLogMapper.insert(log);
            return count > 0 ? true : false;
        } else if (type == 2) {//积分抵扣 平台不足以抵扣 扣除全部
            MememberIntegralLog log = new MememberIntegralLog();
            log.setThirdUserId(thirdUserId);
            log.setMemberUserId(member.getId());
            log.setIntegral(integral);
            log.setActionType(2);
            log.setActionTypeDes("积分修改");
            log.setCreateTime(new Date());
            log.setUpdateTime(new Date());
            log.setChangeTypes(changeTypes);
            log.setChangeTime(new Date());
            log.setStoreMember(1);
            log.setDeptId(member.getDeptId());
            long count = mememberIntegralLogMapper.insert(log);
            MememberIntegralLog updParam = new MememberIntegralLog();
            updParam.setDeptId(member.getDeptId());
            //todo  置为失效字段
//            mememberIntegralLogMapper.update(updParam);
            return count > 0 ? true : false;
        } else if (type == 3) {//消耗本平台 且有剩余 需按时间规则处理
            MememberIntegralLog log = new MememberIntegralLog();
            log.setThirdUserId(thirdUserId);
            log.setMemberUserId(member.getId());
            log.setIntegral(integral);
            log.setActionType(2);
            log.setActionTypeDes("积分修改");
            log.setCreateTime(new Date());
            log.setUpdateTime(new Date());
            log.setChangeTypes(changeTypes);
            log.setChangeTime(new Date());
            log.setStoreMember(1);
            log.setDeptId(member.getDeptId());
            long count = mememberIntegralLogMapper.insert(log);
            //处理log失效时间排序
            MememberIntegralLog updParam = new MememberIntegralLog();
            updParam.setDeptId(member.getDeptId());


            return count > 0 ? true : false;
        } else {
            return false;
        }

    }
}
